package com.hl.card.credit.pub.utils;

import java.lang.Character.UnicodeBlock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends org.apache.commons.lang.StringUtils {
	/**
	 * 数组转换成字符串，按","分割
	 * 
	 * @param array
	 * @return
	 */
	public static String arrayToStr(Integer[] array) {
		return arrayToStr(array, ",");
	}

	/**
	 * 数组转换成int[]，按指定符号分割
	 * 
	 * @param array
	 * @param regx
	 *            分隔符
	 * @return
	 */
	public static int[] arrayToInt(String array) {
		if (array == null || array.length() < 1) {
			return new int[0];
		}
		String[] x = array.split(",");
		int[] y = new int[x.length];
		for (int i = 0; i < x.length; i++) {
			y[i] = Integer.parseInt(x[i]);
		}
		return y;
	}

	/**
	 * 数组转换成字符串，按指定符号分割
	 * 
	 * @param array
	 * @param regx
	 *            分隔符
	 * @return
	 */
	public static String arrayToStr(Integer[] array, String regx) {
		if (array == null || array.length < 1) {
			return null;
		}
		StringBuilder builder = new StringBuilder();
		for (Integer ii : array) {
			builder.append(ii).append(regx);
		}
		return builder.toString();
	}

	/**
	 * 判断一个字符串里面是否有汉字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isChinese(String str) {
		return str.getBytes().length == str.length();
	}
	
	
	/**
	 * 是否全部为汉字
	 * 全部为汉字则为true,否则返回false
	 * @param strName
	 * @return
	 */
	public static boolean isAllChinese(String strName) {
	    char[] ch = strName.toCharArray();
	    if(ch.length == 0){
	    	return false;
	    }
	    for (int i = 0; i < ch.length; i++) {
	        char c = ch[i];
	        if (!isChinese(c)) {
	            return false;
	        }
	    }
	    return true;
	}
	
	public static boolean isChinese(char c) {
	    UnicodeBlock ub = UnicodeBlock.of(c);
	    if (ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
	            || ub == UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
	            || ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
	            || ub == UnicodeBlock.GENERAL_PUNCTUATION
	            || ub == UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
	            || ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
	        return true;
	    }
	    return false;
	}
	
	/**
	 * 判断字符串里是否包含汉字
	 * 此方法只是判断是否是由数字与字母组成
	 * @param str
	 * @return true 包含汉字（非数字与字母）；false 不包含(数字与字母)
	 */
	public static boolean isContainChinese(String str) {
		String reg = "^[A-Za-z0-9]+$";
		if(Pattern.matches(reg, str)){
			return false;
		}else{
			return true;
		}
		
//		if(str.getBytes().length == str.length()){
//			return false;
//		}else{
//			return true;
//		}
	}
	
	/**
	 * 去掉字符串所有的空格
	 * @param str
	 * @return
	 */
	public static String trimAll(String str){
		
		return str.replaceAll("\\s*", "");
	}
	
/*	public static boolean isEmojiCharacter(String strValue) {
		Boolean bool = strValue.matches("[^\\u4e00-\\u9fa5^\\d^A-Z^a-z^0-9]");
		if (strValue != null) {
			char[] codePoints = strValue.toCharArray();
			for (int i = 0; i < codePoints.length; i++) {
				char codePoint = codePoints[i];
				bool = (codePoint == 0x0) || (codePoint == 0x9)
						|| (codePoint == 0xA) || (codePoint == 0xD)
						|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
						|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
						|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
				if (bool) {
					break;
				}
			}
		}
		return bool;
	}*/


	
	 public static int getWordCount(String s)
    {
        s = s.replaceAll("[^\\x00-\\xff]", "**");
        int length = s.length();
        return length;
    }
	
	 public static boolean isNumeric(String str){ 
		   Pattern pattern = Pattern.compile("[0-9]*"); 
		   Matcher isNum = pattern.matcher(str);
		   if( !isNum.matches() ){
		       return false; 
		   } 
		   return true; 
		}
	 
	 /**
	  * 字符串转int数组 逗号隔开 
	  * @param str
	  * @return 
	  */
	public static Integer[] str2IntArr(String str) {
		if (str != null && str != "" && str.contains(",")) {
			String[] strid = str.split(",");
			Integer[] id = new Integer[strid.length];
			for (int i = 0; i < strid.length; i++) {
				id[i] = Integer.parseInt(strid[i]);
			}
			return id;
		} else {
			if (isEmpty(str)) {
				return new Integer[0];	
			}
			return new Integer[]{Integer.parseInt(str)};
		}
	}
	 
	public static void main(String[] args) {
		String nickname="1我的地";
//		checkNickNake(nickname);
		System.out.println(getWordCount(nickname));
		String str1 = "45564dfhiudh";
		String str2 = "45564dfhiudh---";
		String str3 = "45564dfh汉字";
		String str4 = "汉字";
		System.out.println(isContainChinese(str1));
		System.out.println(isContainChinese(str2));
		System.out.println(isContainChinese(str3));
		System.out.println(isContainChinese(str4));
		System.out.println(isContainChinese(null));
		System.out.println(isContainChinese(""));
	}
	public static String reAccount(String account) {
		if (account.contains("@")) {

			int atIndex = account.indexOf("@");
			System.out.println("atIndex="+atIndex);
			String A = account.substring(0, atIndex);
			System.out.println("A="+A);
			if (atIndex>3) {
				A = A.substring(0,3)+"******";
				System.out.println("atIndex>3  >> A=" +A);
			}else {
				A= A+"******";
				System.out.println("atIndex<3  >> A=" +A);
			}
			account = A + account.substring(atIndex);
			System.out.println("Str="+A );
		}else if(account.length()==11){
			account = account.substring(0, 3)+"****"+account.substring(account.length()-4);
			System.out.println(account);
		}
		return account;
	}
	public static String utf8ToUnicode(String inStr) {
        char[] myBuffer = inStr.toCharArray();
        
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < inStr.length(); i++) {
         UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]);
            if(ub == UnicodeBlock.BASIC_LATIN){
             //英文及数字等
             sb.append(myBuffer[i]);
            }else if(ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){
             //全角半角字符
             int j = (int) myBuffer[i] - 65248;
             sb.append((char)j);
            }else{
             //汉字
             short s = (short) myBuffer[i];
                String hexS = Integer.toHexString(s);
                String unicode = "\\u"+hexS;
             sb.append(unicode.toLowerCase());
            }
        }
        return sb.toString();
    }
}
